<?php

use App\Facades\DeviceManager;
use App\Facades\UserManager;
use App\Facades\WeChatService;

if(!function_exists('formatResponse')){

    /**
     * 格式化响应数据
     *
     * @param   string|int      $errCode
     * @param   string          $message
     * @param   mixed           $data
     * @return  array
     */
    function formatResponse($errCode = 0, $message = null, $data = null){

        $response = [
            'status' => [
                'errCode' => $errCode,
            ],
        ];
        
        if($errCode != 0){
            if(!is_null($message)){
                array_set($response, 'status.message', $message); 
            }
        }

        if(!is_null($data)){
            array_set($response, 'data', $data); 
        }

        return $response;
    }
}

if(!function_exists('formatJsonResponse')){

    /**
     * 格式化响应数据
     *
     * @param   mixed           $data
     * @param   string          $message
     * @param   string|int      $errCode
	 * @return  \Illuminate\Http\JsonResponse
     */
    function formatJsonResponse($data = null, $message = '正常', $errCode = 0){
        return Response::json(formatResponse($errCode, $message, $data));
    }
}

if(!function_exists('wxConfig')){
    /**
     * 在返回页面的数据中加入微信jssdk配置信息
     *
     * @param   mixed           $data
     * @param   string          $message
     * @param   string|int      $errCode
     * @return  \Illuminate\Http\JsonResponse
     */
    function wxConfig($data = [], $request){
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

        $jssdkSignPackage = WeChatService::getJssdkSignPackage($url);

        return (array_merge($data, [
            "jssdkSignPackage" => $jssdkSignPackage,
        ]));
    }
}

if(!function_exists('deviceCode')){
    /**
     * 获取用户当前控制设备编码
     * @return string 设备编码
     */
    function deviceCode($type){
        $user = UserManager::getUser();
        if(!is_null($user)){
            $deviceTypeObject = DeviceManager::getDeviceTypeByQuery('type', $type);
            $user->userDevices->load(['device' => function($query){
                $query->select('id', 'type_id', 'code');
            }]);
            foreach($user->userDevices as $userDevice){
                if($userDevice->device->type_id == $deviceTypeObject->id){
                    return $userDevice->device->code;
                }
            }
        }
    }
}

if(!function_exists('isDebugging')){

    /**
     * 检测是否调试模式
     *
     * @return bool
     */
    function isDebugging(){
        
        return Config::get('app.debug'); 
    }
}

if (!function_exists('filterToArray')) {
    function filterToArray($data,$key,$value)
    {       
        $array = [];
        foreach($data as $object)
        {   
            $array = array_add($array, $object->$key, $object->$value);
        }   
        return $array;
    }
}

